﻿
using Newtonsoft.Json;
using SpiderStudy.Utils.Currency;
using SpiderStudy.Utils.EditHelper.CatUtils.Utils.EditHelper;
using SpiderStudy.Utils.Html;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SpiderStudy.WebSite.Baidu
{
    class BaiduBaikeTool
    {
        public string BaikeConent(string Tags, int Retry = 3)
        {

            Root JsonInfo = GetBaikeInfo(Tags);
            if (JsonInfo == null || (JsonInfo.key == null && JsonInfo.desc == null))
            {
                int SleepRandom = new Random().Next(20, 180);
                Console.WriteLine("初始化JSON失败,随机模拟用户睡眠{0}", SleepRandom);
                Thread.Sleep(SleepRandom * 1000);

                while (Retry-- > 0)
                {
                    Console.WriteLine("{0}剩余尝试次数：{1}", Tags, Retry);
                    return BaikeConent(Tags, Retry);
                }
                return "暂无简介。";
            }
            string Content = new HtmlCodeCreate().LableP(string.Format("{0}是{1}。", JsonInfo.key, JsonInfo.desc), "tip");


            Console.WriteLine(JsonInfo.Articleabstract);
            Content += new HtmlCodeCreate().LableP(CreateNameList(JsonInfo.card), "info") + new HtmlCodeCreate().LableP(JsonInfo.Articleabstract, "info");


            Content = new HtmlHelper().RemoveAllAttributes(Content, "//a");

            return Content;

        }
        /// <summary>
        /// 创建列表名字
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        private string CreateNameList(List<CardItem> card)
        {
            List<string> NameList = new List<string>();
            string Mode = "{0}：{1}";
            string TempContent = "";
            if(card!=null)
            foreach (CardItem SingleCard in card) {
;
                string TempNameList = "";
                foreach (string Line in SingleCard.value) {
                    TempNameList += string.Format("{0},",Line);
                }
                TempContent += string.Format(Mode, SingleCard.name, TempNameList);
            }

            return TempContent;
          
        }

        private Root GetBaikeInfo(string Tags)
        {
            string API = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?appid=379020&bk_key={0}";
            string Shtml = WebHelper.HttpGet(string.Format(API,new TextHelper().UrlEncodeToUtf8(Tags),Tags));
            try { 

                return JsonConvert.DeserializeObject<Root>(Shtml.Replace("\"abstract\"", "Articleabstract"));
            } catch (Exception ex) {
                PrintLog.Log(ex);
            }
            return null;
        }

        public class CardItem
        {
            /// <summary>
            /// 
            /// </summary>
            public string key { get; set; }
            /// <summary>
            /// 中文名
            /// </summary>
            public string name { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public List<string> value { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public List<string> format { get; set; }
        }

        public class Root
        {
            /// <summary>
            /// 
            /// </summary>
            public int id { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int subLemmaId { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int newLemmaId { get; set; }
            /// <summary>
            /// 初音未来
            /// </summary>
            public string key { get; set; }
            /// <summary>
            /// 日本VOCALOID虚拟歌手
            /// </summary>
            public string desc { get; set; }
            /// <summary>
            /// 初音未来
            /// </summary>
            public string title { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public List<CardItem> card { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string image { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string src { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int imageHeight { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int imageWidth { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string isSummaryPic { get; set; }
            /// <summary>
            /// 初音未来（初音ミク/Hatsune Miku），是2007年8月31日由CRYPTON FUTURE MEDIA以Yamaha的VOCALOID系列语音合成程序为基础开发的音源库，音源数据资料采样于日本声优藤田咲。2010年4月30日，发布初音未来6种不同声调的版本“初音未来Append”。2013年8月31日，初音未来英文版本同VOCALOID3一并发行。此外，初音未来还担任日本音乐团体Soun...
            /// </summary>
            public string Articleabstract { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<int> moduleIds { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string url { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string wapUrl { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int hasOther { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string totalUrl { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> catalog { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> wapCatalog { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string logo { get; set; }
        /// <summary>
        /// 以上内容来自百度百科平台，由百度百科网友创作。
        /// </summary>
        public string copyrights { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string customImg { get; set; }
        /// <summary>
        /// 
        /// </summary>
       // public List<string> redirect { get; set; }
    }
}
}
